package com.heima.search.web;

import com.heima.search.pojo.RequestParams;
import com.heima.search.service.SearchService;
import com.hmall.common.result.PageResult;
import com.hmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 数据的导入，缓存预热 DB--->es
     *
     * @return
     */
    @GetMapping("/dbToEsDoc")
    public Result pageDoc() {
        searchService.dbToEs();
        return Result.success("db数据导入es成功！！");
    }

    @GetMapping("/pageDoc")
    public Result page() {
        searchService.pageTest();
        return Result.success("部分导入测试");
    }

    /**
     * 不使用多线程测试
     */
    @GetMapping("/noPool")
    public Result noPool() {
        searchService.noPool();
        return Result.success("db===========成功！！");
    }

    /**
     * es中items的数据分页 搜索 排序 过滤等操作
     */
    @PostMapping("/list")
    public PageResult list(@RequestBody RequestParams params) {
        return searchService.listItems(params);
    }

    /**
     * items的 桶的分类 需要使用suggestion
     */
    @PostMapping("/filters")
    public Map<String, List<String>> filters(@RequestBody RequestParams params) {
        return searchService.filters(params);
    }

    /**
     * 自动补齐搜索
     */
    @GetMapping("/suggestion")
    public List<String> suggestion(@RequestParam("key") String key) {
        log.info("搜索前缀是：{}", key);
        return searchService.suggestion(key);
    }
}
